home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
simulato
/
v2_3_mc6.tz
/
v2_3_mc6
/
testfiles
/
samples.asm
< prev
next >
Wrap
Assembly Source File
|
1994-05-02
|
6KB
|
185 lines
;Routines
;
;============================================================================
; I M P O R T A N T!!!
; These routines 'obey' the convention that 'proper' subroutines
; may freely modify registers d0,d1,a0, and a1. All other registers
; must be preserved.
;
; Since most of these subroutines call the kernal, you cannot tell directly
; which registers these subroutines modify. SO IT IS IN YOUR BEST INTEREST
; TO OBEY THE ABOVE CONVENTION. In other words, don't depend on something
; you put in A0,A1,D0,D1 to be there after you call one of these routines.
;
; Also, labels like 1$, 2$ etc. are local labels. They take the form nnn$
; and are only valid between properly named lables. This means that if you
; use these subroutines, you only have to worry about what the NAMES of the
; subroutines are. You do NOT have to worry about all the branches. Also
; note that the source code is harder to understand without properly named
; labels, so it is suggested they not be used in your program.
;
; WARNING: Labels are CASE SENSITIVE, JSR READCHAR is not JSR ReadChar
;
;============================================================================
; equates for kernel routines
INCHE equ 247
OUTCH equ 248
OUTPUT equ 243
;===========================================================================
;
; ReadChar - will read in a character from the keyboard and put it in the
; low-byte of d0. This routine will not return until a character
; has been entered.
; USAGE : JSR ReadChar
;
ReadChar move.l d7,-(sp) ; save d7
move #INCHE,d7
trap #14
move.l (sp)+,d7 ; restore d7
rts
;============================================================================
;
; WriteString - given the starting address of a string in A0, this routine
; will write the string to the screen. The string must be
; terminated by a null byte ($00).
; USAGE : JSR WriteString
; Given : 1. A0 = Address of first character in string
; 2. Last character in string is $00 or NULL
;
;
WriteString movem.l a0-a6/d0-d7,-(sp) ; save registers
move.l a0,a1 ; a0 is overwritten when OUTPUT is called
; so keep track of location within the
; string using a1
movea.l a0,a5
movea.l a0,a6
move #OUTPUT,d7
1$ adda.l #1,a6
move.b (a1)+,d0 ; get a byte
tst.b d0 ; check for null
beq 2$
bra 1$
2$ trap #14 ; print it
movem.l (sp)+,d7-d0/a6-a0 ; restore registers
rts
;===========================================================================
;
; ClearScreen - Clears the screen of the terminal
; USAGE : JSR ClearScreen
;
ClearScreen move.b #$1A,d0
jsr WriteChar
rts
;==========================================================================
;WriteEOL - This character writes an EOL (End of Line) to the screen.
; I.E. So the next string written to the screen doesn't go
; on the end of the last one you've written.
; USAGE : JSR WriteEOL
WriteEOL move.l d7,-(sp)
move #OUTCH,d7
move #13,d0
trap #14
move #10,d0
trap #14
move.l (sp)+,d7
rts
;===========================================================================
; WriteChar - will write a single character contained in the low byte
; of d0 to the screen
;
WriteChar move.l d7,-(sp) ;save d7
move #OUTCH,d7
trap #14
move.l (sp)+,d7 ;restore d7
rts
;===========================================================================
;
; Malloc - (short for Memory ALLOCate) call with register d0 (long)
; holding the size of the memory block requested. A pointer
; to a block of memory will be returned in register A0. The
; start address of the memory block will always be on a word
; boundary. The memory 'heap' that Malloc uses is from
; $5000 to $5FFF. If a Malloc cannot handle a request (lack
; of available memory), it will print an error message and
; terminate the process.
;
malloc
move.l 1$,a0 ; address of the top of the 'heap'
move.l a0,d1
add.l d0,d1 ; add req. size to top
btst #0,d1 ; check if result is odd
beq.s 2$
addq #1,d1 ; make new top an even address
2$ cmp.l #$6000,d1
bgt.s 3$ ; abort if top > $6000
move.l d1,1$ ; save new top
rts
; abort
3$ lea 4$,a0
jsr WriteString
move #228,d7
trap #14
1$ dc.l $5000 ; address of the top of the heap
4$ dc.b 13,10,'NO MEMORY AVAILABLE FOR MALLOC',13,10,0
cnop 0,2
;===========================================================================
;
; ReadString - will read in a string from the keyboard and put it in
; memory pointed to by a0. This routine will not return until
; a carriage return has been entered. A null byte will be moved
; to the end of your string.
;
ReadString move.l d7,-(sp) ; save d7
movea.l a0,a1
move #INCHE,d7
2$ trap #14
jsr WriteChar ;echo the character
cmp.b #13,d0
beq 1$
cmp.b #8,d0 ;compare to backspace
beq 3$
move.b d0,(a1)+
bra 2$
1$ move.b #0,(a1)+ ;move the null to memory
move.l (sp)+,d7 ; restore d7
rts
3$ suba.l #1,a1
bra 2$